package cn.tedu.hm.controller;

import cn.tedu.hm.pojo.Admin;
import cn.tedu.hm.service.AdminService;
import cn.tedu.hm.wep.JsonPage;
import cn.tedu.hm.wep.JsonResult;
import cn.tedu.hm.wep.WebConsts;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 *说明
 *
 *@author:TanXulLiang
 *@data:2023/2/17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user/")
@Api(tags="01. 用户管理模块")
@Transactional
public class UserController {


    @Autowired(required = false)
    AdminService adminService;

    @ApiOperation("查询用户")
    @ApiOperationSupport(order = 100)
    @GetMapping ("list")
    public JsonResult<List<Admin>> select(){
        return  JsonResult.ok(adminService.list());
    }
    @ApiOperation("新增用户")
    @ApiOperationSupport(order = 200)
    @PostMapping("addUser")
    public JsonResult<Void> addNew(Admin admin){
        adminService.save(admin);
        return  JsonResult.ok();
    }
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 300)
    @GetMapping("/delete/{id}")
    public JsonResult<Void> delete(@PathVariable Integer id){
        adminService.removeById(id);
        return  JsonResult.ok();
    }
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "10")
    })
    @GetMapping("/page")
    public JsonResult<JsonPage<Admin>> page(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize){
        JsonPage<Admin> list=adminService.list(page,pageSize);
        return  JsonResult.ok(list);
    }

    @ApiOperation("修改用户")
    @ApiOperationSupport(order = 500)
    @PostMapping("/update/")
    public JsonResult<Void> update(Admin admin){
        log.debug("admin:{}",admin);
        adminService.updateById(admin);
        return  JsonResult.ok();
    }


}
